﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectStorageDB;
using ProjectStorageDB.Objects;

namespace ProjectStorageLib.Project
{
    public class ProjectManager : DatabaseEntities
    {
        protected ProjectStorageDB.Project m_Project;
        public ProjectStorageDB.Project Project { get { return m_Project; } }

        public ProjectManager(Decimal id)
            : base()
        {
            m_Project = this.ent.Projects.Where(p => p.ProjectID == id).First();
        }

        internal ProjectManager(ProjectStorageDB.Project project)
            : base()
        {
            m_Project = project;
            this.ent.Projects.AddObject(project);
        }
        internal ProjectManager(DatabaseEntities manager, ProjectStorageDB.Project project)
            : base(manager)
        {
            m_Project = project;
        }

        public ProjectManager()
            : base()
        {
        }

        public static ProjectManager CreateNewAndGetProject(decimal userID)
        {
            return new ProjectManager(new ProjectStorageDB.Project() { UserID = userID });
        }

        protected override void Dispose(bool flag)
        {

        }

        public IQueryable<ProjectStorageDB.Factor> Factors
        {
            get { return ent.Factors; }
        }
        public IQueryable<ProjectStorageDB.FactorCategory> FactorsCategories
        {
            get { return ent.FactorCategories; }
        }

        public IQueryable<ProjectStorageDB.AgilePractice> AgilePractices
        {
            get { return ent.AgilePractices; }
        }
        public IQueryable<ProjectStorageDB.AgileMethod> AgileMethods
        {
            get { return ent.AgileMethods; }
        }

        public ProjectStorageDB.ProjectFactorScale GetScaleValueByFactor(ProjectStorageDB.Factor i_Factor)
        {
            return m_Project.FactorValues.FirstOrDefault(a => a.FactorID == i_Factor.FactorID);
        }
        public void SetScaleValueByFactor(decimal i_decFactorID, decimal i_decScaleID)
        {
            ProjectStorageDB.ProjectFactorScale factorScale =
                m_Project.FactorValues.FirstOrDefault(a => a.FactorID == i_decFactorID);
            if (factorScale == null)
            {
                var newFacScaleVal = new ProjectFactorScale()
                {
                    FactorID = i_decFactorID
                    ,
                    FactorScaleID = i_decScaleID
                    ,
                    ProjectID = Project.ProjectID
                };

                //newFacScaleVal.Factor = Factors.Where(f => f.FactorID == i_decFactorID).First();
                //newFacScaleVal.FactorScale = ent.FactorScales.Where(fc => fc.FactorScaleID == i_decScaleID).First();
                //newFacScaleVal.Project = Project;

                //this.ent.ProjectFactorScales.AddObject(newFacScaleVal);

                m_Project.FactorValues.Add(newFacScaleVal);
            }
            else
            {
                factorScale.FactorScaleID = i_decScaleID;
            }
        }
        public void DeleteFactor(decimal i_decFactorID)
        {
            ProjectStorageDB.ProjectFactorScale factorScale =
                m_Project.FactorValues.FirstOrDefault(a => a.FactorID == i_decFactorID);
            if (factorScale != null)
            {
                this.ent.DeleteObject(factorScale);
            }
        }

        public ProjectStorageDB.ProjectPractice GetPracticeValueByPractice(AgilePractice i_Practice)
        {
            return m_Project.Practices.FirstOrDefault(p => p.PracticeID == i_Practice.PracticeID);
        }
        public void SetPracticeValueByFactor(decimal i_practiceID, EnumPracticeUsingValue i_value)
        {
            ProjectStorageDB.ProjectPractice practiceValue =
                m_Project.Practices.FirstOrDefault(a => a.PracticeID == i_practiceID);
            if (practiceValue == null)
            {
                var newFacScaleVal = new ProjectPractice()
                {
                    PracticeID = i_practiceID
                    ,WrappedValue = i_value
                    ,ProjectID = Project.ProjectID
                };

                //this.ent.ProjectPractices.AddObject(newFacScaleVal);

                m_Project.Practices.Add(newFacScaleVal);
            }
            else
            {
                practiceValue.WrappedValue = i_value;
            }
        }
        public void DeletePractice(decimal i_practiceID)
        {
            ProjectStorageDB.ProjectPractice practiceValue =
                 m_Project.Practices.FirstOrDefault(a => a.PracticeID == i_practiceID);
            if (practiceValue != null)
            {
                this.ent.DeleteObject(practiceValue);
            }
        }

        public void Save()
        {
            this.ent.SaveChanges();
        }

        public Boolean ExistsProject(string name)
        {
            return this.ent.Projects.Any(p => p.Name == name
                && (m_Project.ProjectID == 0 || p.ProjectID != m_Project.ProjectID));
        }

        public void DeleteProject()
        {
            try
            {
                ent.Projects.DeleteObject(Project);
                ent.SaveChanges();
            }
            catch (Exception) { throw; }
        }

        public String Name
        {
            get { return m_Project.Name; }
            set { m_Project.Name = value; }
        }
        public String Description
        {
            get { return m_Project.Description; }
            set { m_Project.Description = value; }
        }
    }
}
